<?php

class Content extends Think
{
    /**
     * 添加内容
     * @param type $data 需要添加的数据
     * @return type 成功返回新增的信息ID
     */
    public function add($data) {
        $data = $this->checkField($data);
        $id = M(MODULE_NAME)->add($data);
        return $id;
    }

    /**
     * 修改内容
     * @param type $data 需要修改的数据
     * @return boolean 影响的记录数或false
     */
    public function save($data) {
        $data = $this->checkField($data);
        $cat = getCategory($data['catid']);
        $urlRule = getCache('Urlrule');
        $data['url'] = geturl($cat, $data, $urlRule);
        $data['url'] = $data['url'][0];
        $res = M(MODULE_NAME)->save($data);
        return $res;
    }

    protected function checkField($data)
    {
        $modelid = getCategory($data['catid'], 'modelid');
        $fields = getCache('Field_' . $modelid);

        foreach ($fields as $key => $val) {
            $setup = json_decode($val['setup'], true);

            if (!empty($fields[$key]['required']) && empty($data[$key])) {
                throw_exception($fields[$key]['name'] . '不能为空！');
            }

            if ($setup['multiple'] || $setup['inputtype'] == 'checkbox') {
                $data[$key] = implode(',', $data[$key]);
            }

            switch ($fields[$key]['type']) {
                case 'groupid':
                case 'checkbox':
                    if (empty($data[$key])) {
                        $data[$key] = '';
                    } else {
                        $data[$key] = implode(',', $data[$key]);
                    }
                    break;
                case 'posid':
                    if (empty($data['posid'])) {
                        $data['posid'] = '';
                    } else {
                        $data['posid'] = '-' . implode('-', $data['posid']) . '-';
                    }
                    break;
                case 'datetime':
                    $data[$key] = strtotime($data[$key]);
                    break;
                case 'relation':
                    $data[$key] = !empty($data[$key]) ? json_encode(array_unique($data[$key])) : '';
                    break;
                case 'textarea':
                    $data[$key] = addslashes($data[$key]);
                    break;
                case 'images':
                case 'files':
                    $name = $key . '_name';
                    $arrdata = array();

                    foreach ($data[$key] as $k => $res) {
                        if (!empty($data[$key][$k])) {
                            $arrdata[$k]['filepath'] = $data[$key][$k];
                            $arrdata[$k]['filename'] = $data[$name][$k];
                        }
                    }

                    $data[$key] = json_encode($arrdata);
                    break;
                case 'datalist':
                    $data = array();
                    foreach ($data[$key] as $item) {
                        $data[] = $item;
                    }
                    $data[$key] = json_encode($data);
                    break;
                case 'editor':
                    //自动提取摘要
                    if (isset($data['add_description']) && $data['description'] == '' && isset($data['content'])) {
                        $content = stripslashes($data['content']);
                        $description_length = intval($data['description_length']);
                        $data['description'] = str_cut(str_replace(array("\r\n", "\t", '[page]', '[/page]', '&ldquo;', '&rdquo;'), '', strip_tags($content)), $description_length);
                        $data['description'] = addslashes($data['description']);
                    }

                    //自动提取缩略图
                    if (isset($data['auto_thumb']) && $data['thumb'] == '' && isset($data['content'])) {
                        $content = $content ? $content : stripslashes($data['content']);
                        $auto_thumb_no = intval($data['auto_thumb_no']) * 3;
                        if (preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches)) {
                            $data['thumb'] = $matches[$auto_thumb_no][0];
                        }
                    }
                    break;
            }
        }

        $data['create_time'] = !empty($data['create_time']) ? $data['create_time'] : time();
        $data['update_time'] = $data['create_time'];
        $data['user_id'] = $_SESSION['adminid'];
        $data['username'] = $_SESSION['admin']['username'];
        $data['lang'] = LANG_ID;
        if ($data['style_color']) {
            $data['style_color'] = 'color:' . $data['style_color'];
        }

        if ($data['style_bold']) {
            $data['style_bold'] = ';font-weight:' . $data['style_bold'];
        }

        if ($data['style_color'] || $data['style_bold']) {
            $data['title_style'] = $data['style_color'] . $data['style_bold'];
        }
        return $data;
    }
}